//
//  HomeOnlineTrainListViewController.m
//  ZJControlsClass
//
//  Created by wandoufo on 2024/12/8.
//

#import "HomeOnlineTrainListViewController.h"
#import "HomeOnlineTrainModel.h"
#import "ZJHomeOnlineTrainListworking.h"
#import "HomeOnlineTrainListTableViewCell.h"
#import "publicNodataView.h"



@interface HomeOnlineTrainListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UIView * alertView;
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * dataSource;
@property (nonatomic,strong) publicNodataView * nodataView;

@end

@implementation HomeOnlineTrainListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self setUpUI];
    
    [self networking];
}

-(void)networking{
    
    
    WeakSelf(self)
    
    
//    __weak typeof(self) weakSelf = self;
    [[ZJHomeOnlineTrainListworking new] dispatchTaskWithParam:@{@"status": @(self.trainStatus)} completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (responseObject && [responseObject[@"code"] integerValue] == 200) {
            [weakSelf.dataSource removeAllObjects];
            
            [weakSelf.tableView.mj_footer endRefreshing];
            [weakSelf.dataSource  addObjectsFromArray:[HomeOnlineTrainModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]]];
          
            if (self.dataSource.count == 0) {
                self.nodataView.hidden = NO;
            }else{
                self.nodataView.hidden = YES;
            }
            
            
            
            [self.tableView reloadData];
            
        }else{
            [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:^{
                
            }];
        }
        [self.tableView.mj_header endRefreshing];

    }];
    
}
-(void)setUpUI{
    
//    [self.view addSubview:self.alertView];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.nodataView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.top.equalTo(self.view);
//        make.top.equalTo(self.alertView.mas_bottom);
    }];
    
    [self.nodataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(StatusBarHeight);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    
    
}






#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    HomeOnlineTrainModel *model = self.dataSource[section];
    return model.courses.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeOnlineTrainListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeOnlineTrainListTableViewCell"];
    if (!cell){
        cell = [[HomeOnlineTrainListTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"HomeOnlineTrainListTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    HomeOnlineTrainModel *model = self.dataSource[indexPath.section];
    cell.model = model.courses[indexPath.row];
    cell.trainModel = model;
    
    return cell;
}

// 添加section header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 28;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return  0.001;
}
// 添加section header视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 28)];
    headerView.backgroundColor = [UIColor clearColor];
    
    UILabel *headerLabel = [[UILabel alloc] init];
    headerLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightBold];
    headerLabel.textColor = [UIColor colorWithHexString:@"181818"];
    [headerView addSubview:headerLabel];
    
    [headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headerView).offset(12);
//        make.centerY.equalTo(headerView);
        make.bottom.equalTo(headerView);
        make.right.equalTo(headerView).offset(-12);
    }];
    
    // 设置businessName
    HomeOnlineTrainModel *model = self.dataSource[section];
    headerLabel.text = model.businessName;
    
    return headerView;
}




#pragma mark -- 懒加载
-(UIView *)alertView{
    if (!_alertView){
        _alertView = [UIView new];
        _alertView.backgroundColor = [UIColor whiteColor];
    }
    return _alertView;
}

-(UITableView *)tableView{
    if (!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor backgroundGrayColor];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//            pageNo = 1;
//            [self.TableViewParamer addEntriesFromDictionary:@{@"pageNo":@(pageNo)}];
            [self networking];
        }];
    }
    return _tableView;
}
-(NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
-(publicNodataView *)nodataView{
    if (!_nodataView) {
        _nodataView = [publicNodataView new];
        _nodataView.hidden = YES;
    }
    return _nodataView;
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
